package com.travel.server.manager.impl.command.project.data;

import com.travel.common.exception.WorkCode;
import com.travel.common.exception.WorkException;
import com.travel.common.util.CollectionUtil;
import com.travel.common.util.DateUtil;
import com.travel.server.manager.dao.ProjectConsultDao;
import com.travel.server.manager.dto.request.project.data.QueryProjectTrendRequest;
import com.travel.server.manager.dto.response.project.data.QueryProjectTrendResponse;
import com.travel.server.manager.dto.response.project.data.QueryProjectTrendResponse.TrendItem;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.ProjectDal;
import com.travel.server.manager.impl.dal.SpecialDal;
import com.travel.server.manager.impl.dal.SpecialDal.SpecialParam;
import com.travel.server.manager.impl.dal.data.ConsultDal;
import com.travel.server.manager.impl.dal.data.ProjectDataDal;
import com.travel.server.manager.model.ProjectSpecial;
import com.travel.server.manager.model.dto.data.ProjectTrendDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/29 12:09
 * @Desc:
 */
@Component
@Slf4j
public class QueryProjectTrendCommand extends AbstractCommand<QueryProjectTrendRequest, QueryProjectTrendResponse> {

    @Autowired
    private SpecialDal specialDal;

    @Autowired
    private ProjectDataDal projectDataDal;

    @Autowired
    private ProjectConsultDao consultDao;

    @Override
    protected void checkInput(QueryProjectTrendRequest request) {
        if(request.getId() == null){
            throwInvalidParam();
        }
        checkTimeInput(request);
    }

    @Override
    public QueryProjectTrendResponse handle(QueryProjectTrendRequest request) {
        checkInput(request);
        QueryProjectTrendResponse response = new QueryProjectTrendResponse();
        List<TrendItem> visitList = new ArrayList<>();
        List<TrendItem> applyList = new ArrayList<>();
        List<TrendItem> orderList = new ArrayList<>();
        List<TrendItem> consulList = new ArrayList<>();

        Date startTime = DateUtil.getDataTime(request.getStartTime());
        Date endTime = DateUtil.getDataEndTime(request.getEndTime());

        SpecialParam param = new SpecialParam();
        param.setProjectIdList(Arrays.asList(request.getId()));
        List<ProjectSpecial> list = specialDal.selectByExample(param);
        if(CollectionUtil.isEmpty(list)){
            throw new WorkException(WorkCode.RECORD_NOT_EXIST);
        }
        List<Integer> specialIdList = new ArrayList<>();
        for(ProjectSpecial projectSpecial : list){
            specialIdList.add(projectSpecial.getId());
        }

        List<ProjectTrendDto> applyCountList = projectDataDal.selectProjectTrend(startTime, endTime, specialIdList, ProjectDataDal.APPLY_ARRAY);
        List<ProjectTrendDto> orderCountList = projectDataDal.selectProjectTrend(startTime, endTime, specialIdList, ProjectDataDal.ORDER_ARRAY);
        List<ProjectTrendDto> consultCountList = consultDao.selectProjectConsult(startTime, endTime);
        Map<String, String> applyMap = makeMap(applyCountList,false);
        Map<String, String> orderMap = makeMap(orderCountList,false);
        Map<String, String> consultMap = makeMap(consultCountList,true);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startTime);
        while(calendar.getTime().before(endTime) || calendar.getTime().equals(endTime)){
            String day = DateUtil.YYYY_MM_DD.format(calendar.getTime());
            TrendItem visit = new TrendItem();
            TrendItem apply = new TrendItem();
            TrendItem order = new TrendItem();
            TrendItem consult = new TrendItem();
            visit.setNum("0");
            visit.setDate(calendar.getTime());
            apply.setNum(StringUtils.isBlank(applyMap.get(day))?"0":applyMap.get(day));
            apply.setDate(calendar.getTime());
            order.setNum(StringUtils.isBlank(orderMap.get(day))?"0":orderMap.get(day));
            order.setDate(calendar.getTime());
            consult.setNum(StringUtils.isBlank(consultMap.get(day))?"0":consultMap.get(day));
            consult.setDate(calendar.getTime());
            visitList.add(visit);
            applyList.add(apply);
            orderList.add(order);
            consulList.add(consult);
            calendar.add(Calendar.DAY_OF_MONTH, 1);
        }

        response.setApplyList(applyList);
        response.setConsulList(consulList);
        response.setOrderList(orderList);
        response.setVisitList(visitList);

        return response;
    }

    private Map<String, String> makeMap(List<ProjectTrendDto> list, boolean isConsult){
        Map<String, String> map = new HashMap<>();
        if(!CollectionUtil.isEmpty(list)) {
            for (ProjectTrendDto projectTrendDto : list) {
                if (isConsult) {
                    map.put(projectTrendDto.getDays(), projectTrendDto.getConsult());
                } else {
                    map.put(projectTrendDto.getDays(), projectTrendDto.getCount() + "");
                }
            }
        }
        return map;
    }
}
